{extend name="public/base" /}

{block name="title"}<title>固定积分配置人员列表</title>{/block}

{block name="pjax_container"}
<style>
 tr>th,
    td {
        text-align: center;
    }

    .btn-group-xs>.btn,
    .btn-xs {
        padding: 5px;
        margin: 0 5px;
    }



    .content-header>.breadcrumb>li {
        color: #999;
    }

    .content-header>.breadcrumb>.active {
        color: #666;
    }

    .content-header {
        height: 51px;

    }

    .btn-primary {
        background-color: #1890FF;
        color: #fff;
        border: none;
        border-radius: 6px !important;
        margin-left: 15px;
        padding: 5px 10px;
        line-height: 24px;
    }

    .box-primary {
        border-top-color: #ecf0f5 !important;
        border-top-width: 16px;

    }

    .btn-primary:hover {
        background-color: #1890FF;
        color: #fff;
        border: none;
        opacity: .9;
        border-radius: 6px !important;
    }

    .delete-one {
        background-color: ff4141 !important;
        margin-left: 15px;
        border-radius: 6px;
    }

    .sreachs {
        background-color: #1890FF;
        color: #fff;
        border: none;
        border-radius: 6px !important;
        margin-left: 15px;
        padding: 7px 15px;
    }

    .type_explode {
        color: #fff;
        background-color: #d9534f;
        border-radius: 6px;
        border: none;
    }
     .nav-tabs-custom{
        margin-top: 16px;
    }



        .modal-content {
        margin-top: 150px;
        /*width: 600px;*/
        /*height: 690px;*/

    }

    .modal-body {
        padding: 27px;
        /*width: 856px;*/
        /*height: 570px;*/
        display: flex;
    }

    .modal-right {
        flex: 1;
        border: 1px solid #eee;
        margin-left: 30px;
    }

    .modal-left {
        height: 100%;
    }

    .modal-right>h4 {
        width: 100%;
        display: flex;
        justify-content: space-around;
    }

    .form-control {
        border-radius: 6px;
    }

    .table-body {
        width: 540px;
        height: 462px;
        border-radius: 6px;
        background-color: #eee;
    }

    .table-left {
        margin-right: 2%;
        width: 49%;
        height: 100%;
        float: left;
        border: 1px solid #eee;
        background-color: #fff;
    }

    .table-right,
    .table-right2 {
        width: 49%;
        height: 100%;
        border: 1px solid #eee;
        float: left;
        padding-top: 10px;
        background-color: #fff;
    }

</style>
<section class="content-header">
    <ol class="breadcrumb">
        <li>积分管理</li>
        <li>固定积分配置</li>
        <li class="active">固定积分配置人员列表</li>
    </ol>
</section>
<section class="content">
    <div class="row">
        <div class="col-md-12">
            <div class="box box-primary">
                <div class="box-header with-border">
                    <div class="pull-left">
                        <div href="#" class="btn btn-sm btn-primary xzrw"><i class="fa fa-save"></i> 新增</div>
                        {:authAction(CONTROLLER_NAME.'/fixed_user_del','delete_all')}
                    </div>
                    <ul class="nav nav-tabs">
                        <li class="pull-right"><a href="javascript:history.back(-1)" class="btn btn-sm" style="padding:10px 2px;"><i class="fa fa-list"></i> 返回</a></li>
                    </ul>
                </div>
                <div class="box-body table-responsive">
                    <table class="table table-bordered table-hover">
                        <tr>
                            <th width="35"><input type="checkbox" class="minimal checkbox-toggle"></th>
                            <th>头像</th>
                            <th>名称</th>
                            <th>部门</th>
                            <th>操作</th>
                        </tr>
                        {volist name="$list" id="v"}
                        <tr>
                            <td><input type="checkbox" name="id[]" value="{$v.id}" class="minimal magic-checkbox checkbox"></td>
                            <td><img src="{$v['avatar']??'/static/avatar.jpg'}" style="width: 50px;height: 50px"/></td>
                            <td>{$v.name}</td>
                            <td>{$v.department_name}</td>
                            <td>
                                {:authAction(CONTROLLER_NAME.'/fixed_user_del','delete_one',['id'=>$v['id']])}
                            </td>
                        </tr>
                        {/volist}
                    </table>
                </div>
                <div class="box-footer clearfix">
                    {$list->render()}
                </div>
            </div>
        </div>
    </div>
</section>
<script>
    $(function(){
        // 列表部分 全选
        $('input[type="checkbox"].minimal, input[type="radio"].minimal').iCheck({
            checkboxClass: 'icheckbox_minimal-blue',
            radioClass: 'iradio_minimal-blue'
        });
        $('.checkbox-toggle').on('ifChecked', function(event){
            var _this = $(this);
            var _table = _this.closest('.table');
            _table.find("tr td input[type='checkbox']").iCheck("check");
        });
        $('.checkbox-toggle').on('ifUnchecked', function(event){
            var _this = $(this);
            var _table = _this.closest('.table');
            _table.find("tr td input[type='checkbox']").iCheck("uncheck");
        });
    });
    function change_status(id) {
        $.ajax({
            url:"{:url('change_status')}",
            type:'post',
            data:{'id':id},
            success:function (res) {
                $.amaran({'message': res.msg});
                if(res.err == 0){
                    window.location.reload()
                }
            }
        });
    }
</script>

<!-- Modal 样式  -->
<link rel="stylesheet" href="__STATIC__/global/an_tree_css/an_tree_css.css">
<!-- Modal 弹出层重写 多选   -->
<div class="modal fade" id="myModal_multiple" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">&times;</span>
                </button>
                <h4 class="modal-title" id="myModalLabel10">人员选择</h4>
            </div>
            <div class="modal-body">
                <div class="modal-left">
                    <div class="modal-left-title" style="margin-bottom: 20px;">
                        <div class="input-group">
                            <input type="text" name="search_user_name" class="form-control" placeholder="">
                            <span class="input-group-btn">
                                <button class="btn btn-info search_btn" type="button"
                                    style="width: 47px; height: 34px; background-color: #1890FF;border: none;outline: none;"><span
                                        class="glyphicon glyphicon-search"></span></button>
                            </span>
                        </div>
                    </div>
                    <div class="input-group table-body">
                        <div class="table-left">
                            <div class="an_tree">

                            </div>
                        </div>
                        <div class="table-right">
                            <div class="an_tree_list">
                                <div class="an_list_title">

                                </div>
                                <div class="an_list_con">
                                    <!-- <div class="an_list_item">
                                        <div class="item_info">
                                            <span class="name">aaa</span>/
                                            <span>88</span>/
                                            <span>部门</span>
                                        </div>
                                        <div class="item_box item_box_check"><i class="fa fa-check"></i></div>
                                    </div> -->
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
                <div class="modal-right">
                    <div class="multiresult_header">
                        <div>已选择<span class="number">0</span>个</div>
                        <div class="header_clear">清空</div>
                    </div>
                    <div class="multiresult_list">
                        <!-- <div class="an_list_item">
                            <div class="item_info">
                                <span class="name">aaa</span>/
                                <span>88</span>/
                                <span>部门</span>
                            </div>
                            <div class="item_box item_box_check"><i class="fa fa-check"></i></div>
                        </div> -->
                        
                    </div>
                </div>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary popups_submit" id="submit2">确认</button>
            </div>
        </div>
    </div>
</div>
<!-- 多选弹窗操作  -->
<script>
    var now_item_multiple="";//名字输入框
    var input_name="";
    var is_piece="";
    $("body").on("click",".xzrw",function () { 
        $('#myModal_multiple').modal('show');
        
    });
    //确认按钮
    $('#myModal_multiple .popups_submit').click(function (e) {
        $('#myModal_multiple').modal('hide');
        var check_all = $("#myModal_multiple .multiresult_list .an_list_item");
        var item_ids = [];
        for (let i = 0; i < check_all.length; i++) {
            item_ids.push(check_all.eq(i).attr("user_id"));
        }
        $.ajax({
            type: 'get',
            url: "{:url('fixed_user_store')}",
            data: {'id' : "{:input('id')}",'user_id' : item_ids},
            dataType:"json",
            success: function (data) {
                if(data.err==0){
                    layui.layer.msg("添加成功");
                    location.reload();
                }else{
                    layui.layer.msg(data.msg);
                    location.reload();
                }
            }
        })
    });
    //当弹窗关闭时执行重置弹窗
    $('#myModal_multiple').on('hidden.bs.modal', function () {
        //执行一些动作...重置弹窗
        $(".an_tree_list .an_list_title").html("");
        $(".an_tree_list .an_list_con").html("");
        $(".tree_txt_active").removeClass("tree_txt_active");
        $(".an_tree_pack").hide();
        $(".an_tree .fa").removeClass("fa-caret-down").addClass("fa-caret-right");
        $("#myModal_multiple input[name='search_user_name']").val("");
        $("#myModal_multiple .header_clear").click();
    });

</script>
<!-- 树结构js操作 多选  -->
<script>
    $("#myModal_multiple .an_tree").on("click", ".fa", function () {
        $(this).toggleClass("fa-caret-down");
        $(this).toggleClass("fa-caret-right");
        $(this).parents(".an_tree_set").eq(0).children(".an_tree_pack").toggle();
    });
    //添加中间人员列表
    $("#myModal_multiple").on("click", ".an_tree_txt,.search_btn", function () {
        $(".an_tree").find(".tree_txt_active").removeClass("tree_txt_active");
        $(this).addClass("tree_txt_active");
        var department_id = $(this).attr("data_id");
        var department_name = $(this).html();
        var user_name = $("#myModal_multiple input[name='search_user_name']").val();
        // console.log(user_name);
        $.ajax({
            type: 'get',
            url: "{:url('base/base_department_per')}",
            data: {'department_id' : department_id,'user_name' : user_name},
            dataType:"json",
            success: function (data) {
                // var json = JSON.parse(data);
                // console.log(data);
                var content = '';
                $.each(data, function (index, value) {
                    if(value.limits==undefined){
                         var limits=0;
                    }else{
                        var limits=value.limits.b;
                    }
                    content += '<div class="an_list_item" user_name="'+value.name+'" user_id="'+value.id+'" limits_b="'+limits+'"><div class="item_info"><span class="name">' + value.name + '</span>/<span>' + value.job_number + '</span>/<span>' + value.department_name + '</span></div><div class="item_box "><i class="fa fa-check"></i></div></div>'
                });
                $('#myModal_multiple .an_list_con').html("");
                $('#myModal_multiple .an_list_con').append(content);
                $('#myModal_multiple .an_list_title').html('<div>'+department_name + data.length + '人</div><div class="all_item_box"><i class="fa fa-check"></i></div>');
                left_add_active();
                check_all();
            }
        })
    });
    // 选中事件
    $("#myModal_multiple .an_tree_list").on("click", ".an_list_item", function () {
        $(this).toggleClass("list_item_check");
        var ischeck = $(this).attr("class");
        if (ischeck.indexOf("list_item_check") >= 0) {
            add_check();
        } else {
            del_check($(this).attr("user_id"));
        }
        check_all();
    });
    //全选按钮
    $("#myModal_multiple .an_tree_list").on("click", ".all_item_box", function () {
        $(this).toggleClass("all_item_box_check");
        var ischeck = $(this).attr("class");
        var all_item = $("#myModal_multiple .an_tree_list .an_list_item");
        if (ischeck.indexOf("all_item_box_check") >= 0) {
            all_item.addClass("list_item_check");
            add_check();
        } else {
            all_item.removeClass("list_item_check")
            for (let i = 0; i < all_item.length; i++) {
                del_check(all_item.eq(i).attr("user_id"));
            }
        }
        check_all();
    });
    //右侧删除事件
    $("#myModal_multiple .multiresult_list").on("click", ".item_box", function () {
        var user_id = $(this).parents(".an_list_item ").attr("user_id");
        del_check(user_id);
        check_all();
    });
    //右侧清空监听
    $("#myModal_multiple .multiresult_header").on("click", ".header_clear", function () {
        var all_item = $("#myModal_multiple .multiresult_list .an_list_item");
        for (let i = 0; i < all_item.length; i++) {
            del_check(all_item.eq(i).attr("user_id"));
        }
        check_all();
    });
    //全选按钮检测
    function check_all(){
        var all_item_num = $("#myModal_multiple .an_tree_list .an_list_item").length;
        var checked_item_num = $("#myModal_multiple .an_tree_list .list_item_check").length;
        if(all_item_num != 0 && all_item_num == checked_item_num){
            $("#myModal_multiple .an_tree_list .all_item_box").addClass("all_item_box_check");
        }else{
            $("#myModal_multiple .an_tree_list .all_item_box").removeClass("all_item_box_check");
        };
        //右侧选中人数统计
        var people_num = $("#myModal_multiple .multiresult_list .an_list_item").length;
        $("#myModal_multiple .multiresult_header .number").html(people_num);
    };
    //添加右侧选中项
    function add_check(){
        var item_ids = [];
        var all_checked_item = $("#myModal_multiple .an_list_con").find(".list_item_check");
        var now_checker_item = $("#myModal_multiple .multiresult_list .an_list_item");
        for (let i = 0; i < now_checker_item.length; i++) {
            item_ids.push(now_checker_item.eq(i).attr("user_id"));
        }
        for (let i = 0; i < all_checked_item.length; i++) {
            var now_id = all_checked_item.eq(i).attr("user_id");
            if ( $.inArray(now_id,item_ids) < 0 ) {
                var new_item = all_checked_item.eq(i).prop("outerHTML");
                new_item = new_item.replace("fa-check","fa-remove");
                new_item = new_item.replace("list_item_check","");
                $("#myModal_multiple .multiresult_list").append(new_item);
            } 
        }
        // console.log(item_ids);
    }
    //左侧第一次生成时检测是否有选中项
    function left_add_active(){
        var left_item_ids = [];
        var left_checked_item = $("#myModal_multiple .an_list_con .an_list_item");
        var right_checker_item = $("#myModal_multiple .multiresult_list .an_list_item");
        for (let i = 0; i < left_checked_item.length; i++) {
            left_item_ids.push(left_checked_item.eq(i).attr("user_id"));
        }
        for (let i = 0; i < right_checker_item.length; i++) {
            var user_id = right_checker_item.eq(i).attr("user_id");
            if ( $.inArray(user_id,left_item_ids) >= 0 ) {
                $("#myModal_multiple .an_list_con .an_list_item[user_id="+user_id+"]").addClass("list_item_check");
            }
        }  
    }
    //取消选中项
    function del_check(user_id){
        var left_item_ids = [];
        var right_item_ids = [];
        var left_checked_item = $("#myModal_multiple .an_list_con .an_list_item");
        var right_checker_item = $("#myModal_multiple .multiresult_list .an_list_item");
        for (let i = 0; i < left_checked_item.length; i++) {
            left_item_ids.push(left_checked_item.eq(i).attr("user_id"));
        }
        for (let i = 0; i < right_checker_item.length; i++) {
            right_item_ids.push(right_checker_item.eq(i).attr("user_id"));
        }
        if ( $.inArray(user_id,left_item_ids) >= 0 ) {
           $("#myModal_multiple .an_list_con .an_list_item[user_id="+user_id+"]").removeClass("list_item_check");
        }
        if ( $.inArray(user_id,right_item_ids) >= 0 ) {
            $("#myModal_multiple .multiresult_list .an_list_item[user_id="+user_id+"]").remove();
        }
    }
</script>
<!-- 递归填充目录 多选  -->
<script>
    var department = "";
    $.ajax({
        type: 'get',
        url: "{:url('base/base_department_page')}",
        dataType:"json",
        async: false,
        success: function (data) {
            // eventList = JSON.parse(data);
            department = data;
        }
    })
    var an_tree = ergodic(department)
    $("#myModal_multiple .an_tree").append(an_tree);
    function ergodic(data) {
        var content = '';
        $.each(data, function (index, value) {
            if (value.children != "") {
                var children = ergodic(value.children);
                content += '<div class="an_tree_set"><div class="an_tree_entry"><div class="an_tree_main"><span class="an_tree_icon"><i class="fa fa-caret-right"></i></span><span class="an_tree_txt" data_id="' + value.id + '" data_pid="' + value.pid + '">' + value.title + '</span></div></div><div class="an_tree_pack">' + children + '</div></div>';
            } else {
                content += '<div class="an_tree_set"><div class="an_tree_entry"><div class="an_tree_main"><span class="an_tree_icon"></span><span class="an_tree_txt" data_id="' + value.id + '" data_pid="' + value.pid + '">' + value.title + '</span></div></div><div class="an_tree_pack"></div></div>';
            }
        });
        return content;
    }
</script>
{/block}
